//
//  NBTabBarController.m
//  聚超值
//
//  Created by 李智恒 on 16/3/27.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBTabBarController.h"
#import "NBNavigationController.h"
#import "UIImage+Image.h"
#import "NBTabBarView.h"

#import "NBHomeViewController.h"
#import "NBDiscoverViewController.h"
#import "NBSquareViewController.h"
#import "NBShowViewController.h"
#import "NBExperienceViewController.h"

@interface NBTabBarController ()<NBTabBarViewDelegate>

@property (nonatomic,strong) NSMutableArray *items;

@end

@implementation NBTabBarController

//懒加载
-(NSMutableArray *)items
{
    if (_items==nil) {
        _items=[NSMutableArray array];
    }
    return _items;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置所有控制器
    [self SetUpAllController];
    
    //使用自定义TabBar
    [self setUpMyTabBar];
    
}

//创建所有控制器方法
-(void)SetUpAllController
{
    //首页
    // 加载storyBoard
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"NBHomeViewController" bundle:nil];
    // 从storyBoard加载控制器
    NBHomeViewController *home= [storyBoard instantiateViewControllerWithIdentifier:@"home"];
    
    [self addOneChildVC:home title:@"首页"  image:[UIImage imageOriginalWithName:@"pcobb_tab_home"] selectImage:[UIImage imageOriginalWithName:@"pcobb_tab_home_hl"]];
    
    
    //发现
    NBDiscoverViewController *discover=[[NBDiscoverViewController alloc]init];
    
    [self addOneChildVC:discover title:@"发现"  image:[UIImage imageOriginalWithName:@"pcobb_tab_discovery"] selectImage:[UIImage imageOriginalWithName:@"pcobb_tab_discovery_hl"]];
    
    
    //广场
    NBSquareViewController *square=[[NBSquareViewController alloc]init];
    
    [self addOneChildVC:square title:@"广场"  image:[UIImage imageOriginalWithName:@"pcobb_tab_plaza"] selectImage:[UIImage imageOriginalWithName:@"pcobb_tab_plaza_hl"]];
    
    
    
    //晒物
    NBShowViewController *show=[[NBShowViewController alloc]init];
    
    [self addOneChildVC:show title:@"晒物"  image:[UIImage imageOriginalWithName:@"pcobb_tab_showOrder"] selectImage:[UIImage imageOriginalWithName:@"pcobb_tab_showOrder_hl"]];
    
    //经验
    NBExperienceViewController *experience=[[NBExperienceViewController alloc]init];
    
    [self addOneChildVC:experience title:@"经验" image:[UIImage imageOriginalWithName:@"pcobb_tab_experience"] selectImage:[UIImage imageOriginalWithName:@"pcobb_tab_experience_hl"]];

}

//创建一个子控制器方法
- (void)addOneChildVC:(UIViewController *)vc title:(NSString *)title  image:(UIImage *)image selectImage:(UIImage *)selImage {
    
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selImage;
    vc.title=title;

    //包装一个导航控制器
    NBNavigationController *nav = [[NBNavigationController alloc] initWithRootViewController:vc];
    
    [self.items addObject:nav.tabBarItem];

    [self addChildViewController:nav];
    
}

//自定义TabBar
-(void)setUpMyTabBar
{
    //移除系统TabBar
    [self.tabBar removeFromSuperview];
    
    NBTabBarView *TabBar=[[NBTabBarView alloc]init];
    
    TabBar.items=self.items;
    
    TabBar.frame=self.tabBar.frame;
    
    TabBar.backgroundColor=[UIColor colorWithRed:96 green:96 blue:96 alpha:1];
    
    TabBar.delegate=self;
    
    [self.view addSubview:TabBar];
    
}

//实现代理方法
-(void)tabbar:(NBTabBarView *)tabbar btnClickIndex:(NSInteger)index
{
    self.selectedIndex=index;
}

@end
